$(document).ready(function () {
    var curPage = 1; //当前页码
    var total, pageSize, totalPage;
//总记录数，每页显示数，总页数
    pageSize = 5;
    getData(1);

    $.ajax({
        type: "POST",
        url: "/question/getCount",
        dataType: "json",
        success: function (data) {
            console.log(data);
            // alert("data=" + data);
            total = data;
            totalPage = total / pageSize;
            if (parseInt(totalPage) == totalPage) {
                totalPage = total / pageSize;
                return;
            }
            else {
                totalPage = (total - total % pageSize) / pageSize + 1;
            }
            // alert("totalPage=" + totalPage);

            $("#pagination1").pagination({
                currentPage: 1,
                totalPage: totalPage,
                callback: function (current) {
                    $("#current1").text(current)
                    $("#t1 tr:not(:first)").empty();
                    getData(current);
                }
            });

        },
        error: function (xhr, textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });


    function getData(page) {
        $.ajax({
            url: "/question/getPage",
            type: 'POST',
            data: {
                'currentPage': page,
                'pageSize': pageSize,
            },
            beforeSend: function () {

            },
            success: function (data) {
                console.log(data);
                for (var i in data) {
                    $("#t1").append("<tr><td>" + data[i].id + "</td><td>" + data[i].content + "</td><td>" +
                        "<input type='button' class='select' value='选择' /> " + "</td></tr>");
                }
                $(".select").click(function () {
                    //alert(1111);
                    // alert($(this).parent().parent().find("td").eq(0).text());
                    //alert($(this).parent().parent().find("td").eq(1).text());
                    $("#tb").append("<tr><td>" + $(this).parent().parent().find("td").eq(0).text()
                        + "</td><td>" + $(this).parent().parent().find("td").eq(1).text() + "</td><td>" +
                        "<input type='button' class='del' value='删除' /> " + "</td>></tr>");
                    $(".del").click(function () {
                        $(this).parent().parent().remove();
                    });

                });
            },
            complete: function () { //生成分页条
                getPageBar();
            },
            error: function () {
                alert("数据加载失败");
            }
        });
    }

//获取分页条
    function getPageBar() {
        $("#pagecount").find('*').remove();
        //页码大于最大页数
        if (curPage > totalPage) curPage = totalPage;
        //页码小于1
        if (curPage < 1) curPage = 1;
        pageStr = "<span>共" + total + "条</span><span>" + curPage + "/" + totalPage + "</span>";

        //如果是第一页
        if (curPage == 1) {
            pageStr += "<span>首页</span><span>上一页</span>";
        } else {
            pageStr += "<span><a href='javascript:void(0)' rel='1'>首页</a></span><span><a href='javascript:void(0)' rel='" + (curPage - 1) + "'>上一页</a></span>";
        }

        //如果是最后页
        if (curPage >= totalPage) {
            pageStr += "<span>下一页</span><span>尾页</span>";
        } else {
            pageStr += "<span><a href='javascript:void(0)' rel='" + (parseInt(curPage) + 1) + "'>下一页</a></span><span><a href='javascript:void(0)' rel='" + totalPage + "'>尾页</a></span>";
        }
        $("#pagecount").append(pageStr);
    }
});